package org.wattdepot.cli.commands;

//import java.util.List;
import java.util.List;
import javax.xml.datatype.XMLGregorianCalendar;
import org.wattdepot.cli.UserCommandInterface;
import org.wattdepot.client.WattDepotClientException;
import org.wattdepot.resource.property.jaxb.Properties;
import org.wattdepot.resource.source.jaxb.Source;

/**
 * Prints out the fuel type from the source.
 * 
 * @author Jarret Mizo, Shaun Ramento
 * 
 */
public class TotalPowerFuelType extends UserCommandInterface implements UserCommands {

  /**
   * Prints information about a single source.
   * 
   * @param args arguments from command line.
   * @return output prints out the result of FuelTypes.
   */
  @Override
  public String doCommand(String args) {
    String[] inputString = args.split(" ");
    String output = null;
    try {
      output = totalPowerFuelType(inputString[1], inputString[2], inputString[4]);
    }
    catch (WattDepotClientException e) {
      System.out.println("Total power fuel error.\n");
    }
    return output;
  }

  /**
   * Gives the value of the power generated from the source.
   * 
   * @param sourceName name of the source.
   * @param time time for TimeStamp.
   * @return buffer prints out the result of FuelTypes.
   * @throws WattDepotClientException throws exception.
   */

  public int powerGenerated(String sourceName, String time) throws WattDepotClientException {
    ValidateDay day = new ValidateDay();
    XMLGregorianCalendar timeStamp = day.validateDay(time);
    return (int) getClient().getPowerGenerated(sourceName, timeStamp);
  }

  /**
   * Prints information about a single source.
   * 
   * @param sourceName name of the source.
   * @param time time for TimeStamp.
   * @param fuelType type of fuel (LSFO, coal, or waste).
   * @return buffer prints total power generated depending on the fuel type.
   * @throws WattDepotClientException throws exception.
   */
  public String totalPowerFuelType(String sourceName, String time, String fuelType)
      throws WattDepotClientException {
    StringBuffer buffer = new StringBuffer(255);
    //verifies if the last argument is LSFO, coal, or waste
    if (!(fuelType.equalsIgnoreCase("coal") || fuelType.equalsIgnoreCase("LSFO") || fuelType
        .equalsIgnoreCase("waste"))) {
      buffer.append("Fuel type must be LSFO, coal, or waste.\n");
      return buffer.toString();
    }
    int totalPowerGenerated = 0;
    int subTotalPowerGenerated = 0;
    int subSubTotalPowerGenerated = 0;
    int counter = 0;
    
    Source source = getClient().getSource(sourceName);
    buffer.append("Fuel Type : " + fuelType + "\n");
    List<String> urlList = source.getSubSources().getHref();
    int urlListSize = urlList.size();
    if (source.isVirtual()) {

      while (counter != urlListSize) {
        String urlName = urlList.get(counter);
        int lastIndex = urlName.lastIndexOf('/');
        String name = urlName.substring(lastIndex + 1);
        Source subSource = getClient().getSource(name);
        //if subsource is virtual
        if ((subSource.isVirtual())) {
          List<String> subUrlList = subSource.getSubSources().getHref();
          int subUrlListSize = subUrlList.size();
          int subCounter = 0;
          while (subCounter != subUrlListSize) {
            String subUrlName = subUrlList.get(subCounter);
            int subLastIndex = subUrlName.lastIndexOf('/');
            String subName = subUrlName.substring(subLastIndex + 1);
            Boolean subTrueFuelType = isNonVirtual(subName, fuelType);
            if (subTrueFuelType) {
              int subPGenerate = powerGenerated(subName, time);
              //gets the sub total power generated 
              subSubTotalPowerGenerated = subPGenerate + subSubTotalPowerGenerated;
            }
            subCounter++;
          }
          counter++;
        }
        else {

          Boolean trueFuelType = isNonVirtual(name, fuelType);
          if (trueFuelType) {
            buffer.append(name + " : " + powerGenerated(name, time) + "\n");
            int pGenerate = powerGenerated(name, time);
            subTotalPowerGenerated = pGenerate + subTotalPowerGenerated;
          }
          counter++;

        }
        totalPowerGenerated = subTotalPowerGenerated + subSubTotalPowerGenerated;
      }
      buffer.append("Total power generated : " + totalPowerGenerated + "\n");
    }
    return buffer.toString();
  }

  /**
   * Verifies if fuelTypeName is the same in the property.
   * 
   * @param sourceName sourceName is for non-virtual source.
   * @param fuelTypeName name of the fuel type.
   * @param samplingInterval 
   * @return turns true if fuelTypeName is equal
   * @throws WattDepotClientException throws exception.
   */
  public Boolean isNonVirtual(String sourceName, String fuelTypeName) 
      throws WattDepotClientException {
    Properties property = getClient().getSource(sourceName).getProperties();
    String propertyString = property.toString();
    int propertyStringLength = propertyString.length();
    //searches for the location of value
    int value = propertyString.lastIndexOf("value");
    String fuelType = propertyString.substring(value + 6, propertyStringLength - 2);
    if (fuelType.equalsIgnoreCase(fuelTypeName)) {
      return true;
    }

      return false;
    
  }

}
